package web.autocomplete;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.PropertyModel;

public class AutocompleteHome extends WebPage{
	private static final long serialVersionUID = 1L;
	private List<String> source;
	private String seleccionado;
	private Form<Void> form;

	public AutocompleteHome() {
		source = Arrays.asList("str1", "str2", "str3");
		form = new Form<Void>("form");
		add(form.add(
			autoCompleteField(),
			texto()
		));
	}

	private Component texto() {
		return new Label("string_texto", new PropertyModel<String>(this, "seleccionado"));
	}

	protected Component autoCompleteField() {
		final AutoCompleteTextField<String> field = new AutoCompleteTextField<String>("string_field", new PropertyModel<String>(this, "seleccionado")) {
			private static final long serialVersionUID = 1L;

			@Override
			protected Iterator<String> getChoices(String input) {
				return comparar(input).iterator();
			}
		};
		
		return field;
	}
	
	protected List<String> comparar(String input) {
		List<String> subList = new ArrayList<String>();

		for(String string : source){
			if(string.contains(input)){
				if(subList.size() == 10) {
					break;
				}
				subList.add(string);
			}
		}
		return subList;
	}

	
	//setters&getters
	public void setSeleccionado(String seleccionado) {
		this.seleccionado = seleccionado;
	}

	public String getSeleccionado() {
		return seleccionado;
	}
}